package com.example.homeworkmutualevaluation.Dao;

import com.example.homeworkmutualevaluation.Entity.Course;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CourseDao extends JpaRepository<Course, String> {
    Course findByCno(String cno);
    @Transactional
    @Modifying
    @Query(value = "INSERT INTO course VALUES (?1, ?2)", nativeQuery = true)
    int insert(String cno, String cname);
    @Query(value = "SELECT course.`cno`, course.`cname` FROM teaching, course WHERE teaching.`tphone`=?1 AND teaching.`cno`=course.`cno`", nativeQuery = true)
    List<Course> findAllByTphone(String tphone);
    @Query(value = "SELECT course.`cno`, course.`cname` FROM sc, course WHERE sc.`sphone`=?1 AND sc.`cno`=course.`cno`", nativeQuery = true)
    List<Course> findAllBySphone(String sphone);
    @Transactional
    @Modifying
    @Query(value = "DELETE FROM course WHERE `cno`=?1", nativeQuery = true)
    int delete(String cno);

    @Query(value = "SELECT course.* FROM course, homework WHERE course.`cno`=homework.`cno` AND homework.`id`=?1", nativeQuery = true)
    Course findByHomeworkid(int homeworkid);
}
